﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.BLL;
using Com.CMS263.PO;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage.ManageUser
{
    public partial class User_action : System.Web.UI.Page
    {
        GuestBLL gB = new GuestBLL();
        public string email = "";
        public string guid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            int id=0;
            string op = Request["op"];
            if (!string.IsNullOrEmpty((Request["id"])))
            {
                id= int.Parse(Request["id"]);
            }           
            if (op.Equals("Valid"))
            {
                gB.entity = (Guest)gB.FindModelById("Guest", id);
                var bools = (gB.entity.IsChecked) ? false : true;
                gB.entity.IsChecked = bools;
                gB.UpDate();
                Response.Write((bools) ? "yes" : "no");
            }
            else if (op.Equals("delMessage"))
            {
                gB.entity = (Guest)gB.FindModelById("Guest", id);
                if (gB.Delete())
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("no");
                }
            }
            else if (op.Equals("getPass"))
            {
                 email = Request["email"];
                //字符串过滤
                email = Com.CMS263.Util.StringUtil.checkStr(email);            

               
                GuestBLL guestBll = new GuestBLL();
                guestBll.entity = guestBll.FindByEmail(email);
                if (guestBll.entity == null)
                {
                    Response.Write("0");
                }
                else
                {
                    var tempID = Guid.NewGuid();
                    ResetGetPassBLL resBll = new ResetGetPassBLL();
                    resBll.entity.IsValid = true;
                    resBll.entity.UserID = email;
                    resBll.entity.PassGuID = tempID.ToString();
                    resBll.Add();
                    EmailSetBLL emSetBll = new EmailSetBLL();
                    if (emSetBll.GetSet().Count > 0)
                    {
                        emSetBll.entity = emSetBll.GetSet()[0];
                    }
                    StringUtil.SendSMTPEMail(emSetBll.entity.Server, emSetBll.entity.MailUser, emSetBll.entity.MailPass, email, "模板 - 更改密码的通知!", "请点击 <a href=http://localhost:10340/manage/ManageUser/User_action.aspx?op=ChangePass?email=" + email + "&guid=" + tempID.ToString() + "\">http://localhost:10340/manage/ManageUser/User_action.aspx?op=&email=" + email + "&guid=" + tempID.ToString() + "</a> 改变你的密码！");
                
                    //Util.EmailUtil.SendMail(guestBll.entity.UserEmail.ToString(), "模板 - 更改密码的通知!", "请点击 <a href=http://localhost:10340/manage/ManageUser/User_action.aspx?op=ChangePass?email=" + email + "&guid=" + tempID.ToString() + "\">http://localhost:10340/manage/ManageUser/User_action.aspx?op=&email=" + email + "&guid=" + tempID.ToString() + "</a> 改变你的密码！");
                    Response.Write("1");
                }
            }
            else if (op.Equals("ChangePass"))
            {
                email = Request.QueryString["email"];
                guid = Request.QueryString["guid"];
                ////字符串过滤
                if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(guid))
                {
                    Response.Redirect("GetPass.aspx");
                }
                else
                {
                    GuestBLL guestBll = new GuestBLL();
                    guestBll.entity = guestBll.FindByEmail(email);
                    if (guestBll.entity == null)
                    {
                        Response.Redirect("GetPass.aspx");
                    }
                    else
                    {
                        ResetGetPassBLL resBll = new ResetGetPassBLL();
                        resBll.entity = resBll.FindModelByGuid(guid);
                        if (resBll.entity != null)
                        {
                             Response.Redirect("GetPass.aspx");
                        }
                        else
                        {
                            Response.Redirect("ChangePassword.aspx");
                        }
                    }               
                }
            }
            else if (op.Equals("ChangePassOver"))
            {
                //字符串过滤
                email=Request["email"];
                string password = Request["password"];
                guid=Request["guid"];
                email = Util.StringUtil.checkStr(email);
                password = Util.StringUtil.checkStr(password);

                GuestBLL guestBll = new GuestBLL();
                guestBll.entity = guestBll.FindByEmail(email);
                if(guestBll.entity != null)
                {
                    ResetGetPassBLL resBll = new ResetGetPassBLL();
                    resBll.entity = resBll.FindModelByGuid(guid);
                    if (resBll.entity!=null)
                    {
                        guestBll.entity.UserPass = Util.StringUtil.Md5(password);
                        guestBll.UpDate();
                        resBll.entity.IsValid = false;
                        resBll.UpDate();
                        Response.Write("1");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
                else
                {
                    ResetGetPassBLL resBll = new ResetGetPassBLL();
                    resBll.entity=resBll.FindModelByGuid(guid);
                    if (resBll.entity!=null)
                    {
                        resBll.entity.IsValid = false;
                        resBll.UpDate();
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }                 
            }        
        }
    }
}
